/*-------------------------------------------------------------------------
***************** Modification History ***********************************
   @doc
   @module Fields2.c - More Transaction Flow functions |
   @end

$Log: /SoftPay/SPK2SIM/Source/Tran/fields2.c $
**************************************************************************

   Copyright (C) 1999 by VeriFone, Inc.

   All rights reserved.  No part of this software may be reproduced,
   transmitted, transcribed, stored in a retrieval system, or translated
   into any language or computer language, in any form or by any means,
   electronic, mechanical, magnetic, optical, chemical, manual or otherwise,
   without the prior written permission of VeriFone, Inc.

---------------------------------------------------------------------------*/
#define VSFIELD2_C

#include <stdlib.h>
#include <applidl.h>
#include <aclconio.h>
#include <acldev.h>
#include <aclstr.h>
#include <dbmgr.h>
#include <string.h>
#include <actutil.h>

#include <project.h>

/** /#define DEBUG_VSFIELD2*/
#ifdef DEBUG_VSFIELD2
#define DEBUG_95
#endif

#include <define.h>
#include <transact.h>
#include <fields.h>
#include <card.h>
#include <confproto.h>
#include <confio.h>
#include <tblutil.h>
#include <msg.h>
#include <msgfuncs.h>
#include <print.h>
#include <tran.h>
#include <date.h>
#include <ui.h>
#include <oper.h>
#include <entry.h>
#include <pscalls.h>
#include <form.h>
#include <pbatch.h>
#include <spdebug.h>
#include <format.h>
#include <multilng.h>
#include <amount.h>
#include <tip.h>
#include <codesec.h>
#include <passwd.h>
#include <menus.h>

/*---------------------------------------------------------------------------
    Function     : inGetAuthCode
    Description  : get the auth code.
    Returns      : VS_ERR, VS_SUCCESS, VS_FAILURE or VS_ESCAPE
---------------------------------------------------------------------------*/
int inGetAuthCode(TRANSACTION_OBJECT* pobTran) {
    char szAuthCode[AUTH_CODE_SIZE + 1];
    char szDisplayBuffer[DISP_STR_LEN + 1];
    int inResult;

    /* Get Auth Code */
    if ((inResult = inEnterWithMessage(AUTH_CODE_MSG, SCROLL_DISPLAY_TRUE, szAuthCode, INPUT_ALNUMS, AUTH_CODE_SIZE, inValidateString)) >= 0) {
        strcpy(pobTran->srBRec.szAuthCode, szAuthCode);
        memset(szDisplayBuffer, 0, sizeof(szDisplayBuffer));

        vdGetMessageFromFile(TRANS_APP_MSG, szDisplayBuffer);
        strcat(szDisplayBuffer, " ");
        strcat(szDisplayBuffer, szAuthCode);
        vdSGRespMsg(szDisplayBuffer);
        inResult = VS_SUCCESS;
    }
    return(inResult);
}


/*---------------------------------------------------------------------------
    Function     : inGetAdjustAmount
    Description  : get the new amount for adjusts.
    Returns      : VS_ERR, VS_SUCCESS or VS_ESCAPE
---------------------------------------------------------------------------*/
#ifndef REMOVE_CORE_GET_ADJUST_AMOUNT

int inGetAdjustAmount(TRANSACTION_OBJECT* pobTran) {
    char szAmount[AMT_SIZE + 1];
    signed int inResult;
    char szAmt[AMT_SIZE + 1];
    char* szDisplaySymbol;
    char szDisplayBuffer[DISP_STR_LEN + 1];
    long lnBaseAmt;
    char* chNull = 0;

    /** /pdebug(("--inGetAdjustAmount--"));*/

    memset(szDisplayBuffer, '\0', sizeof(szDisplayBuffer));
    szAmount[0] = '\0';

    szDisplaySymbol = szGetCurSymbol();

    /* save old amount */
    pobTran->srBRec.lnOldTotalTransactionAmount = pobTran->srBKey.lnTotalOfTransactionAmount;
    pobTran->srBRec.lnOldTipAmount = pobTran->srBRec.lnTipAmount;  /*kc1*/
    lnBaseAmt = (pobTran->srBKey.lnTotalOfTransactionAmount - pobTran->srBRec.lnTaxAmount - pobTran->srBRec.lnTipAmount - pobTran->srBRec.lnCashBackAmount);

    sprintf(szAmt, SUBSTITUTE_LD, lnBaseAmt); /* mb1 */
    vdFormatAmount(szAmt, VS_FALSE);
    vdTrimLeadSpaces(szAmt);

    vdGetMessageFromFile(SHOW_AMOUNT_MSG, szDisplayBuffer);
    strcat(szDisplayBuffer, " ");
    vdDisplayAt(obDisp.inHomeCol, obDisp.inDetailLine, szDisplayBuffer, CLR_EOL);

    //  vdDispStrAtRight(szAmt, obDisp.inDetailLine, NO_CLEAR);
    if (getgrid() == 0)
        vdDisplayDataStr(szAmt, obDisp.inStatusLine, NO_CLEAR, ML_RIGHT);
    else
        vdDisplayDataStr(szAmt, obDisp.inDetailLine, NO_CLEAR, ML_RIGHT);

    vdGetMessageFromFile(NEWAMT_MSG11, szDisplayBuffer);
    vdDisplayAt(obDisp.inHomeCol, obDisp.inInputLine, szDisplayBuffer, CLR_EOL);

    //  BT_TRACE ('I', szDisplayBuffer);
    if ((inResult = inGetAmount(szAmount, VS_TRUE, 1L, 999999999L, szDisplaySymbol)) != VS_SUCCESS)
        return(inResult);

    pobTran->srBRec.lnBaseTransactionAmount = atol(szAmount);

    if (fGetTaxProc() == VS_TRUE) {
        pobTran->fTaxAdjust = VS_TRUE;
        if (lnGetTaxPercent() > 0) {
            pobTran->lnOldTaxAmount = pobTran->srBRec.lnTaxAmount;
            inComputePercent(lnGetTaxPercent(), szAmount, pobTran->srBRec.lnBaseTransactionAmount, TAX_FORMAT); /* BB1 DFW#121 01/20/98 */
            pobTran->srBRec.lnTaxAmount = atol(szAmount);
        }
    }

    /* betty_b1 12/18/97 */
    if (inGetTipPercent() > 0 && pobTran->srBRec.inCode != PHONE) /* bb1 */ {
        if ((inResult = obDisp.inDO_ChooseOne(chNull, pchGetMsg(ASK_AUTO_UPDATE_TIP_MSG15), pchGetMsg(YESNO_MSG5x), 2, NULL_PFI)) == KEY_YES) {
            inComputePercent((long) inGetTipPercent(), szAmount, pobTran->srBRec.lnBaseTransactionAmount, TIP_FORMAT);
            pobTran->srBRec.lnTipAmount = atol(szAmount);
            pobTran->fTipAdjust = VS_TRUE; /* bb1 */
        }
    }

    pobTran->srBKey.lnTotalOfTransactionAmount = pobTran->srBRec.lnBaseTransactionAmount + pobTran->srBRec.lnTipAmount + pobTran->srBRec.lnTaxAmount + pobTran->srBRec.lnCashBackAmount;

    return (VS_SUCCESS);
}

#endif /* #ifndef REMOVE_CORE_GET_ADJUST_AMOUNT */


/*-------------------------------------------------------------------------
   Function :    fExcessiveTab
    Description :   Determines if Tab entered is too much based on Excessive
        Tab Percentage in TCT table and the Open Tab Amount
    Parameters :
    Returns :   VS_TRUE, VS_FALSE
    Globals :
    Notes :
--------------------------------------------------------------------------*/
VS_BOOL fExcessiveTab(TRANSACTION_OBJECT* pobTran,long lnTabAmt) {
    int inRetVal = VS_FALSE;
    char szCalcAmt[FORMATTED_AMOUNT_SIZE + 1];
    long lnCalcAmt;

    if (inGetExcessTab() > 0 && pobTran->srBRec.fCloseTabSameCard) {
        inComputePercent((long) inGetExcessTab(), szCalcAmt, pobTran->srBRec.lnBaseTransactionAmount, WHOLE_PERCENT_FORMAT);
        lnCalcAmt = str2long(szCalcAmt);
        if (lnTabAmt >= (lnCalcAmt + pobTran->srBRec.lnBaseTransactionAmount))
            inRetVal = VS_TRUE;
    }

    return (inRetVal);
}


/*---------------------------------------------------------------------------
    Function     : inGetCloseTabAmount
    Description  : get the new amount for close tab.
    Returns      : VS_ERR, VS_SUCCESS or VS_ESCAPE
 ---------------------------------------------------------------------------*/
#ifndef REMOVE_CORE_GET_CLOSE_TAB_AMOUNT
int inGetCloseTabAmount(TRANSACTION_OBJECT* pobTran) {
    char szAmount[AMT_SIZE + 1];
    int inResult,
    inAmtLen;
    char szAmt[AMT_SIZE + 1];
    char* szDisplaySymbol;
    char szDisplayBuffer[DISP_STR_LEN + 1];
    long lnBaseAmt;
    long lnTabAmt;

    /** /pdebug(("--inGetAdjustAmount--"));*/

    memset(szDisplayBuffer, '\0', sizeof(szDisplayBuffer));
    szAmount[0] = '\0';

    szDisplaySymbol = szGetCurSymbol();

    /* save old amount */
    pobTran->srBRec.lnOldTotalTransactionAmount = pobTran->srBKey.lnTotalOfTransactionAmount;
    pobTran->srBRec.lnOldTipAmount = pobTran->srBRec.lnTipAmount;  /*kc1*/
    lnBaseAmt = (pobTran->srBKey.lnTotalOfTransactionAmount - pobTran->srBRec.lnTaxAmount - pobTran->srBRec.lnTipAmount - pobTran->srBRec.lnCashBackAmount);

    sprintf(szAmt, SUBSTITUTE_LD, lnBaseAmt); /* mb1 */
    vdFormatAmount(szAmt, VS_FALSE);
    vdTrimLeadSpaces(szAmt);

    vdGetMessageFromFile(SHOW_AMOUNT_MSG, szDisplayBuffer);
    pad(szDisplayBuffer, szDisplayBuffer, ' ', obDisp.inCharsPerRow, LEFT);
    inAmtLen = strlen(szAmt);
    strncpy(szDisplayBuffer + (obDisp.inCharsPerRow - inAmtLen), szAmt, inAmtLen);
    vdDisplayString(szDisplayBuffer);
    vdSetNextLineEntry();
    vdSetNextLineCleanup();
    do {
        vdGetMessageFromFile(NEWAMT_MSG11, szDisplayBuffer);
        vdDisplayAt(obDisp.inHomeCol, obDisp.inCharsPerColumn, szDisplayBuffer, CLR_EOL);

        memset(szAmount, 0, sizeof(szAmount));
        vdGetMessageFromFile(NEWAMT_MSG11, szDisplayBuffer);
        //        BT_TRACE ('I', szDisplayBuffer);
        if ((inResult = inGetAmount(szAmount, VS_TRUE, 1L, 999999999L, szDisplaySymbol)) != VS_SUCCESS)
            return(inResult);

        lnTabAmt = atol(szAmount);

        if (fExcessiveTab(pobTran, lnTabAmt)) {
            // Coding this instead of using ERROR_MSG because we
            // need to be consistant with the key presses used in
            // Batch Review - see review.c / inBatchReviewTipAdjust()
            vdDisplayMessageFromFile(TOO_MUCH_TAB_MSG, STATUS_MSG);
            vdBeep();
            vdFlushKeyboardBuffer();
            inResult = inWaitKey(inGetIdleTimeOut());

            if (inResult != KEY_CANCEL) {
                if (inGetPwdFromUser(ACCESS_WITH_MANAGER_PASSWORD, NO_PIT_INDEX) != VS_ESCAPE) {
                    pobTran->srBRec.lnBaseTransactionAmount = lnTabAmt;
                    inResult = VS_SUCCESS;
                } else
                    return(VS_ESCAPE);
            } else {
                // Try again
                clrscr();
                continue;
            }
        } else {
            pobTran->srBRec.lnBaseTransactionAmount = lnTabAmt;
            inResult = VS_SUCCESS;
        }
    } while (inResult != VS_SUCCESS && !act_kbd_pending_test(KEY_CANCEL));


    vdClrNextLineEntry();
    //    pobTran->srBRec.lnBaseTransactionAmount = atol(szAmount);

    return (VS_SUCCESS);
}

#endif /* #ifndef REMOVE_CORE_GET_CLOSE_TAB_AMOUNT */

/*---------------------------------------------------------------------------
    Function     : inGetBaseAmount
    Description  : get the base amount for the transaction.
    Returns      : VS_ERR, VS_SUCCESS or VS_ESCAPE
 ---------------------------------------------------------------------------*/
#ifndef REMOVE_CORE_GET_BASE_AMOUNT
int inGetBaseAmount(TRANSACTION_OBJECT* pobTran) {
    char szAmount[AMT_SIZE + 1];
    int inResult;
    int inTranCode,
    inOpenAmount;
    char* szDisplaySymbol;
    char szDisplayBuffer[DISP_STR_LEN + 1];

    if (pobTran->srBRec.lnBaseTransactionAmount > 0)
        return (VS_SUCCESS);

    inTranCode = pobTran->srBRec.inCode;

    inOpenAmount = 0;
    vdDisplayMessageFromFile(AMT_MSG, ENTRY_MSG);
    szAmount[0] = '\0';

    if (inTranCode == PRE_AUTH) {
        inOpenAmount = inGetOpenTabDAmt();
        inOpenAmount = inOpenAmount * 100;
        sprintf(szAmount, SUBSTITUTE_D, inOpenAmount);
    }

    szDisplaySymbol = szGetCurSymbol();
    vdGetMessageFromFile(AMT_MSG, szDisplayBuffer);
    //   BT_TRACE ('I', szDisplayBuffer);
    if ((inResult = inGetAmount(szAmount, VS_TRUE, 1L, 999999999L, szDisplaySymbol)) != VS_SUCCESS)
        return(inResult);
    pobTran->srBRec.lnBaseTransactionAmount = atol(szAmount);

    return (VS_SUCCESS);
}
#endif /* #ifndef  REMOVE_CORE_GET_BASE_AMOUNT */
/*---------------------------------------------------------------------------
    Function     : inGetTotalAmount
    Description  : get the total amount for the transaction.
    Returns      : VS_ERR, VS_SUCCESS or VS_ESCAPE
 ---------------------------------------------------------------------------*/
int inGetTotalAmount(TRANSACTION_OBJECT* pobTran) {
    char szAmount[AMT_SIZE + 1];
    int inResult;
    char* szDisplaySymbol;
    char szDisplayBuffer[DISP_STR_LEN + 1];

    vdDisplayMessageFromFile(AMT_MSG, ENTRY_MSG);
    szAmount[0] = '\0';

    szDisplaySymbol = szGetCurSymbol();
    vdGetMessageFromFile(AMT_MSG, szDisplayBuffer);
    //    BT_TRACE ('I', szDisplayBuffer);
    if ((inResult = inGetAmount(szAmount, VS_TRUE, 1L, 999999999L, szDisplaySymbol)) != VS_SUCCESS)
        return(inResult);

    pobTran->srBKey.lnTotalOfTransactionAmount = atol(szAmount);

    return (VS_SUCCESS);
}

/*-------------------------------------------------------------------------
    Function :    inGetBatchNumber
    Description : This function checks if there is transaction record
                  corresponding to an invoice number exists in the Batch.
                  Called in the VOID/ADJUST transaction flow.
    Parameters :
    Returns :
    Notes :
--------------------------------------------------------------------------*/

int inGetBatchNumber(TRANSACTION_OBJECT* pobTran) {
    char szBatchNumber[BATCH_NUM_SIZE + 1];
    char szLocBuf[1 + 1];
    int inResult;
    int inRowLim = inGetRowLim();

    szBatchNumber[0] = '\0';
    memset(szLocBuf, NULL_CH, sizeof(szLocBuf));

    inResult = inEnterWithMessage(BATCH_NUM_MSG, SCROLL_DISPLAY_FALSE, szBatchNumber, INPUT_NUMS, inGetBatchNumSize(), NULL_PFI);
    window(obDisp.inHomeCol, obDisp.inTitleLine, inRowLim, obDisp.inCharsPerColumn);

    if (inResult <= 0) {
        vdSGErrorMessage(TXN_CANCELLED);
        return (VS_ERR);
    }

    strcpy(pobTran->szBatchNum, szBatchNumber);
    pad(pobTran->szBatchNum, pobTran->szBatchNum, '0', inGetBatchNumSize(), RIGHT);

    strlcpy(pobTran->szSettDate, szLocBuf, DATE_SIZE);
    return (VS_SUCCESS);
}


/*---------------------------------------------------------------------------
    Function     : inGetCashBack
    Description  : get the cash back for the transaction.
    Returns      : VS_ERR, VS_SUCCESS or VS_ESCAPE
 ---------------------------------------------------------------------------*/
#ifndef REMOVE_CORE_GET_CASH_BACK
int inGetCashBack(TRANSACTION_OBJECT* pobTran) {
    char szAmount[AMT_SIZE + 1];
    int inResult;
    char* szDisplaySymbol;
    char szDisplayBuffer[DISP_STR_LEN + 1];

    if (fGetCashBack() && (pobTran->srBRec.inTranCardType == DEBIT_CARD || pobTran->srBRec.inTranCardType == EBT_CARD) && inGGCode() != REFUND_DEBIT) {
        vdDisplayMessageFromFile(CASHBKAMT_MSG9, ENTRY_MSG);
        szAmount[0] = '\0';

        szDisplaySymbol = szGetCurSymbol();
        vdGetMessageFromFile(CASHBKAMT_MSG9, szDisplayBuffer);
        //    BT_TRACE ('I', szDisplayBuffer);
        if ((inResult = inGetAmount(szAmount, VS_TRUE, 0L, 999999999L, szDisplaySymbol)) != VS_SUCCESS)
            return(inResult);
        pobTran->srBRec.lnCashBackAmount = atol(szAmount);
    }
    return (VS_SUCCESS);
}
#endif /* #ifndef REMOVE_CORE_GET_CASH_BACK */

#ifndef REMOVE_CORE_GET_CONFIRMATION

int inGetConfirmation(TRANSACTION_OBJECT* pobTran) {
    int inChar;

    vdDisplayMessageFromFile(CONTINUE_TRANS_MSG, STATUS_MSG);
    while (((inChar = get_char()) != KEY_CR) && (inChar != KEY_CANCEL)); // BJD: KEY_CLR -> KEY_CANCEL
    if (inChar == KEY_CR)
        return (VS_SUCCESS);
    else {
        vdSGErrorMessage(TXN_CANCELLED);
        return (VS_ERR);
    }
}
#endif /* #ifndef REMOVE_CORE_GET_CONFIRMATION */



/*-------------------------------------------------------------------------
    Function :    inGetHostNum
    Description :
    Parameters :
    Returns :
    Notes :
--------------------------------------------------------------------------*/

int inGetHostNum(TRANSACTION_OBJECT* pobTran) {
    int inHostIndex;
    char* chNull = 0;

    /** /pdebug (("--inGetHostNum--"));*/

    if ((inHostIndex = inSelectHost(-1, chNull, inCredDebBatchExists, BATCH_EMPTY_MSG)) >= 0) {
        /* if MIT record not already loaded, load first merchant record */
        if (inGetMerchantNum() == 0)
            inLoadMITRec(0);

        if (inLoadDefaultHost(inHostIndex) != VS_SUCCESS)
            return (VS_ERR);

        if (inLoadDefaultCurrency(pobTran) != VS_SUCCESS)
            return (VS_ERR);
        vdSetHostHandle(inHostIndex);
    } else if (inHostIndex == VS_ESCAPE)
        return(VS_ESCAPE);
    else {
        pdebug(("inHostIndex=%d", inHostIndex));
        vdSGErrorMessage(INV_HOST_MSG);
        return(VS_ERR);
    }

    return (VS_SUCCESS);
}




/*-------------------------------------------------------------------------
    Function :    inGetCurrency
    Description :
    Parameters :
    Returns :
    Notes :
--------------------------------------------------------------------------*/

int inGetCurrency(TRANSACTION_OBJECT* pobTran) {
    int inCurrencyIndex;
    char* chNull = 0;

    /** /pdebug (("--inGetCurrency--"));*/

    if (!fGetAllowMultiCurrency())
        return (VS_SUCCESS);

    /** /pdebug (("HDT MultiCurr=%d",fGetHDTAllowMultiCurr()));*/

    if (!fGetAllowMultiCurr())
        return (VS_SUCCESS);

    if ((inCurrencyIndex = inSelectCurrency(-1, chNull, inCSTExists)) >= 0) {
        if (inLoadCSTRec(inCurrencyIndex) != VS_SUCCESS) {
            vdSGErrorMessage(CST_ERR_MSG);
            return(VS_ERR);
        }
    } else {
        if (inCurrencyIndex == VS_ESCAPE)
            vdSGErrorMessage(0L);               // Reset any errors on CLEAR KEY
        else
            vdSGErrorMessage(CST_ERR_MSG);
        return (VS_ERR);
    }

    pobTran->srBKey.inCurrencyIndex = shGetCurrencyIndex(); /*ALVARO*/

    return (VS_SUCCESS);
}


/*-------------------------------------------------------------------------
    Function :    inGetIndBatchHostNum
    Description :
    Parameters :
    Returns :
    Notes :
--------------------------------------------------------------------------*/

int inGetIndBatchHostNum(TRANSACTION_OBJECT* pobTran) {
    int inHostIndex;
    char* chNull = 0;

    /** /pdebug (("--inGetHostNum--"));*/

    if ((inHostIndex = inSelectHost(-1, chNull, inIndBatchExists, BATCH_EMPTY_MSG)) >= 0) {
        if (inLoadDefaultHost(inHostIndex) != VS_SUCCESS)
            return (VS_ERR);

        if (inLoadDefaultCurrency(pobTran) != VS_SUCCESS)
            return (VS_ERR);
        vdSetHostHandle(inHostIndex);
    } else if (inHostIndex == VS_ESCAPE)
        return(VS_ESCAPE);
    else {
        pdebug(("inHostIndex=%d", inHostIndex));
        vdSGErrorMessage(INV_HOST_MSG);
        return(VS_ERR);
    }

    return (VS_SUCCESS);
}




/*-------------------------------------------------------------------------
    Function :    inGetCkHostNum
    Description : Provides for selection of check host.
--------------------------------------------------------------------------*/
int inGetCkHostNum(TRANSACTION_OBJECT* pobTran) {
    int inHostIndex;
    char* chNull = 0;

    if ((inHostIndex = inSelectHost(-1, chNull, inCkHostExists, TRANS_NOT_SUPPORTED_MSG)) >= 0) {
        /*
         * all check-host-specific settings...
         */
        pobTran->srBRec.inTranCardType = NON_CARD;
        vdSGCurCDTIndex(NONCARD_HOST);

        if (inLoadDefaultHost(inHostIndex) != VS_SUCCESS)
            return(VS_ERR);

        if (inLoadDefaultCurrency(pobTran) != VS_SUCCESS)
            return(VS_ERR);
        vdSetHostHandle(inHostIndex);
    } else if (inHostIndex == VS_ESCAPE)
        return(VS_ESCAPE);
    else {
        pdebug(("inHostIndex=%d", inHostIndex));
        vdSGErrorMessage(INV_HOST_MSG);
        return(VS_ERR);
    }

    return(VS_SUCCESS);
}


/*-------------------------------------------------------------------------
    Function :    inGetHFTHostNum
    Description : Provides for selection of correct HFT.
--------------------------------------------------------------------------*/
int inGetHFTHostNum(TRANSACTION_OBJECT* pobTran) {
    int inHostIndex;
    char* chNull = 0;

    if ((inHostIndex = inSelectHFTHost(-1, chNull, inHFTHostExists)) >= 0) {
        if (inLoadHDTRec(inHostIndex) != VS_SUCCESS)
            return(VS_ERR);

        if (inLoadDefaultCurrency(pobTran) != VS_SUCCESS)
            return(VS_ERR);
    } else if (inHostIndex == VS_ESCAPE)
        return(VS_ESCAPE);
    else {
        pdebug(("inHostIndex=%d", inHostIndex));
        vdSGErrorMessage(INV_HOST_MSG);
        return(VS_ERR);
    }

    return(inHostIndex);
}


/*-------------------------------------------------------------------------
    Function :    inGetInvoiceNumber
    Description : This function checks if there is transaction record
                  corresponding to an invoice number exists in the Batch.
                  Called in the VOID/ADJUST transaction flow.
    Parameters :
    Returns :
    Notes :
--------------------------------------------------------------------------*/

int inGetInvoiceNumber(TRANSACTION_OBJECT* pobTran) {
    char szInvoiceNumber[INV_NUM_SIZE + 1];
    int inResult;
    int inRowLim = inGetRowLim();


    inResult = inEnterWithMessage(INVOICE_MSG, SCROLL_DISPLAY_FALSE, szInvoiceNumber, INPUT_NUMS, inGetInvNumSize(), inValInvoiceEntry);

    window(obDisp.inHomeCol, obDisp.inTitleLine, inRowLim, obDisp.inCharsPerColumn);

    if (inResult == VS_ESCAPE)
        return (VS_ESCAPE);

    if (inResult < 0)
        return (VS_ERR);

    pobTran->srBKey.lnInvNum = atol(szInvoiceNumber);

    return (VS_SUCCESS);
}

/*-------------------------------------------------------------------------
    Function :    inGetLastFourAcct
    Description : This function checks if there is transaction record
                  corresponding to last 4 digits of account number in the
                  batch.  Called in the VOID transaction flow.
    Parameters :
    Returns :
    Notes :
--------------------------------------------------------------------------*/

int inGetLastFourAcct(TRANSACTION_OBJECT* pobTran) {
    char szSAcct[LAST4_DIGIT_ACCT + 1];
    int inResult;
    int inRowLim = inGetRowLim();

    memset(szSAcct, 0, sizeof(szSAcct));

    /* Get last four digits of account number */

    szSAcct[0] = '\0';
    obDisp.inStringMinLength = 4;
    inResult = inEnterWithMessage(LAST_FOUR_MSG, SCROLL_DISPLAY_FALSE, szSAcct, INPUT_NUMS, LAST4_DIGIT_ACCT, inValStringMinLength);
    window(obDisp.inHomeCol, obDisp.inTitleLine, inRowLim, obDisp.inCharsPerColumn);

    if (inResult == VS_ESCAPE) {
        vdSGErrorMessage(TXN_CANCELLED);
        return (VS_ESCAPE);
    }

    if (inResult <= 0) {
        vdSGErrorMessage(TXN_CANCELLED);
        return (VS_ERR);
    }

    /* Copy to Global Structure srGlblBKey for retrieval */
    //strlcpy(pobTran->srBKey.SAcctNum, szSAcct,(FIVE_DIGIT_ACCT + 1));
    pobTran->srBKey.lnLast4 = str2long(szSAcct);

    return (VS_SUCCESS);
}

/*-------------------------------------------------------------------------
    Function :    inGetSettDate
    Description : This function checks if there is transaction record
                  corresponding to an invoice number exists in the Batch.
                  Called in the VOID/ADJUST transaction flow.
    Parameters :
    Returns :
    Notes :
--------------------------------------------------------------------------*/

int inGetSettDate(TRANSACTION_OBJECT* pobTran) {
    char szSettDate[DATE_SIZE + 1],
    szScratch[DATE_SIZE + 1] ;
    int inResult;
    int inRowLim = inGetRowLim();

    /* Get settlement date */

    szSettDate[0] = '\0';

    vdSetNextLineEntry();

    inResult = inEnterWithMessage(SETT_DATE_MSG, SCROLL_DISPLAY_FALSE, szSettDate, INPUT_NUMS, 6, inValidateOrigDate);

    vdClrNextLineEntry();

    window(obDisp.inHomeCol, obDisp.inTitleLine, inRowLim, obDisp.inCharsPerColumn);

    if (inResult == VS_ESCAPE)
        return (VS_ESCAPE);

    if (strlen(szSettDate) < 6) {
        vdSGErrorMessage(INVALID_ENTRY_MSG);
        return (VS_ERR);
    }

    pobTran->szBatchNum[0] = '\0';
    pad(szScratch, szSettDate, '0', DATE_SIZE, RIGHT);
    strcpy(szSettDate, szScratch);
    szSettDate[6] = '\0';
    strlcpy(pobTran->szSettDate, szSettDate, DATE_SIZE);
    return (VS_SUCCESS);
}

/*-------------------------------------------------------------------------
    Function :    inGetSettAll
    Description : If Reprint Settlement report for all then set key values
                  to zeros.
    Parameters :  void
    Returns :     VS_SUCCESS OR VS_ERR
--------------------------------------------------------------------------*/
int inGetSettAll(TRANSACTION_OBJECT* pobTran) {
    char szLocBuf[1 + 1];

    memset(szLocBuf, NULL_CH, sizeof(szLocBuf));

    pobTran->szBatchNum[0] = '\0';
    strlcpy(pobTran->szSettDate, szLocBuf, DATE_SIZE);

    if (act_kbd_pending_test(KEY_CANCEL))
        return(VS_ESCAPE);

    return (VS_SUCCESS);
}

/*---------------------------------------------------------------------------
    Function :    inGetRoomNumer()
    Description : This function obtains the room number from the user
    Parameters :
    Returns :     VS_ERR, VS_ABORT or VS_SUCCESS
    Notes :
 ---------------------------------------------------------------------------*/
#ifndef REMOVE_CORE_GET_ROOM_NUMBER
int inGetRoomNumber(TRANSACTION_OBJECT* pobTran) {
    int inResult;
    char szRoomNum[ROOM_NUM_SIZE + 1];

    /** /pdebug(("--inGetRoomNumber--"));*/

    memset(szRoomNum, '\0', sizeof(szRoomNum));
    vdClearNonTitleLines();
    if ((inResult = inEnterWithMessage(ROOM_NUM_MSG, SCROLL_DISPLAY_FALSE, pobTran->srBRec.szRoomNum, INPUT_ALNUMS, ROOM_NUM_SIZE, inValidateString)) > 0) {
        pobTran->srBRec.fRoomNum = VS_TRUE;
        inResult = VS_SUCCESS;
    }

    return(inResult);
}
#endif /* #ifndef REMOVE_CORE_GET_ROOM_NUMBER */


/*---------------------------------------------------------------------------
    Function :    inGetInvNum()
    Description : This function obtains the invoice number from the user if
                  the key invoice flag is on.
    Parameters :
    Returns :     VS_ERR or VS_SUCCESS
    Notes :
 ---------------------------------------------------------------------------*/

int inGetInvNum(TRANSACTION_OBJECT* pobTran) {
    char szInvoiceNumber[INV_NUM_SIZE + 1];
    int inResult;
    VS_BOOL fScrollDisplay = !(obDisp.blNoScrollEntries);

    int inRowLim = inGetRowLim();

    /** /pdebug(("--inGetInvNum--"));*/

    inResult = VS_ERR;

    if (fGetKeyInvNum() == VS_FALSE) {
        /*
         *      if the transaction is a Close Tab and Offline (which means the card
         *      number is the same as the Open Tab) then use the invoice number in the batch record
         */
        if (!pobTran->srBRec.fCloseTabSameCard)
            pobTran->srBKey.lnInvNum = lnGetInvNum();
        return(VS_SUCCESS);
    }

    /* If the transaction is Close Tab and card number is the same as the
     * Open Tab, exit.
     * Else if the card number is different (it was changed to online) there
     * needs to be a new invoice number.
     */
    if (pobTran->srBRec.fCloseTabSameCard)
        return(VS_SUCCESS);

    while (TRUE) {
        if (inResult == VS_SUCCESS)
            vdDisplayMessageFromFile(DUPLICATE_INV_NUM_MSG, WARNING_MSG);

        szInvoiceNumber[0] = '\0';
        inResult = inEnterWithMessage(INVOICE_MSG, fScrollDisplay, szInvoiceNumber, INPUT_NUMS, inGetInvNumSize(), inValidateString);
        window(obDisp.inHomeCol, obDisp.inTitleLine, inRowLim, obDisp.inCharsPerColumn);
        fScrollDisplay = SCROLL_DISPLAY_FALSE;

        if (inResult < 0) {
            vdSGErrorMessage(0L);
            return (VS_ESCAPE);
        }

        if (atol(szInvoiceNumber) == 0L) {
            vdDisplayMessageFromFile(INVALID_INV_NUM_ERR_MSG, WARNING_MSG);
            inResult = VS_ERR;
            continue;
        }

        /* Search in the Open Tab Batch for same Invoice Number and Host Type */
        /* Search in the Host Batch for same Invoice Number */
        pobTran->srBKey.lnInvNum = atol(szInvoiceNumber);

#ifdef BATCH_DEFINED
        inResult = inCheckForDuplicate(pobTran);
#endif // BATCH_DEFINED

        if (inResult != VS_SUCCESS)
            break;
    } /* while (TRUE) */

    vdSetInvNum(pobTran->srBKey.lnInvNum);

    vdSGErrorMessage(0L);

    return(VS_SUCCESS);
}

/*---------------------------------------------------------------------------
    Function     : inGetOrigTransDate
    Description  : get the original transaction date for the transaction.
    Returns      : VS_ERR, VS_SUCCESS or VS_ESCAPE
 ---------------------------------------------------------------------------*/
#ifndef REMOVE_CORE_GET_ORIG_TRANS_DATE

int inGetOrigTransDate(TRANSACTION_OBJECT* pobTran) {
    char szDate[ORIG_TRANS_DATE_SIZE + 1];

    if (pobTran->srBRec.inTranCardType == DEBIT_CARD) {
        vdClearStatusMessage();
        if (inEnterWithMessage(ORIG_TRANS_DATE_MSG, SCROLL_DISPLAY_TRUE, szDate, INPUT_NUMS, EXP_DATE_SIZE, inValidateOrigDate) < 0)
            return (VS_ERR);
        strlcpy(pobTran->szOrigTransDate, szDate, ORIG_TRANS_DATE_SIZE);
        strlcpy(srRcpt.unFrm.srReceipt.szOrigTransDate, szDate, ORIG_TRANS_DATE_SIZE);
    }
    return (VS_SUCCESS);
}
#endif  /* #ifndef REMOVE_CORE_GET_ORIG_TRANS_DATE */

/*---------------------------------------------------------------------------
    Function :    inGetCustNum()
    Description : This function obtains the customer number from the user if
                  this is a PURCHASE_CARD transaction.
    Parameters :
    Returns :     VS_ERR or VS_SUCCESS
    Notes :
 ---------------------------------------------------------------------------*/

int inGetCustNumber(TRANSACTION_OBJECT* pobTran) {
    int inResult = VS_SUCCESS;
    char szCustNum[REF_CUST_SIZE + 1];

    /** /pdebug(("--inGetCustNumber--"));*/

    if (pobTran->srBRec.inTranCardType == PURCHASE_CARD) {
        memset(szCustNum, '\0', sizeof(szCustNum));
        vdClearNonTitleLines();
        vdSetNextLineEntry();   /* for 3200 */

        /* so that user data entered will not be below next input line, like amount */
        vdSetNextLineCleanup();

        if ((inResult = inEnterWithMessage(CUST_NUM_MSG, SCROLL_DISPLAY_FALSE, szCustNum, INPUT_ALNUMS, REF_CUST_SIZE, NULL_PFI)) > 0) {
            pobTran->srBRec.fCustNum = VS_TRUE;
            strlcpy(pobTran->srBRec.szCustNum, szCustNum, REF_CUST_SIZE);
            inResult = VS_SUCCESS;
        }
        vdClrNextLineEntry();
    }

    return(inResult);
} /* End of inGetCustNumber () */

/*********************************************************************************************
* @func int | inSetCloseTabSameCardFlag |
*       Sets the CloseTabSameCard Flag in the batch record to TRUE.
*
* @parm TRANSACTION_OBJECT *    | pobTran |
*
* @rdesc Return values.
* @flag int    |  VS_SUCCESS
* @end
**********************************************************************************************/
int inSetCloseTabSameCardFlag(TRANSACTION_OBJECT* pobTran) {
    pobTran->srBRec.fCloseTabSameCard = VS_TRUE;
    return(VS_SUCCESS);
}


/*-------------------------------------------------------------------------
    Function:       inIncInvNum
    Description:    Increment the Invoice number of the MIT
    Parameters:
    Returns:        VS_ERR, VS_SUCCESS
    Notes:
--------------------------------------------------------------------------*/

int inIncInvNum() {
    long lnInvNum;

    //pdebug (("--inIncInvNum--"));

    lnInvNum = lnGetInvNum();

    if (++lnInvNum > lnMaxValue(inGetInvNumSize()))
        lnInvNum = 1L;

    //pdebug (("lnInvNum = %d", lnInvNum));
    vdSetInvNum(lnInvNum);

    if (inSaveMITRec(inGetMITIndex()) != VS_SUCCESS)
        return(VS_ERR);

    return(VS_SUCCESS);
}

/*-------------------------------------------------------------------------
    Function:       inResetInvNum
    Description:    Reset the Invoice number of the MIT
    Parameters:
    Returns:        VS_ERR, VS_SUCCESS
    Notes:
--------------------------------------------------------------------------*/

int inResetInvNum() {
    long lnInvNum;

    //pdebug (("--inResetInvNum--"));

    lnInvNum = 1L;

    //pdebug (("lnInvNum = %d", lnInvNum));
    vdSetInvNum(lnInvNum);

    if (inSaveMITRec(inGetMITIndex()) != VS_SUCCESS)
        return(VS_ERR);

    return(VS_SUCCESS);
}

/*-------------------------------------------------------------------------
    Function :    inGetWebMenu
    Description : Calls Web menu depending on tran type and set card allowed
                  flag only if a credit card is used.
    Parameters :
    Returns :   VS_SUCCESS, VS_ESCAPE
    Globals :
    Notes :
    Modification History :
      #     Date      Who                     Description
    ----- -------- ---------- ---------------------------------------------
      0   12/4/00  Jeannie_C1 Created
--------------------------------------------------------------------------*/
#ifndef REMOVE_CORE_GET_WEB_MENU
int inGetWebMenu(TRANSACTION_OBJECT* pobTran) {
    int inCardAllowed = 0;  /* only if a credit card is used*/
    int inRetVal = VS_SUCCESS;

    pdebug(("--inGetWebMenu--"));

    if (pobTran->srBRec.fManual == VS_FALSE)
        return(VS_SUCCESS);

    inCardAllowed = (pobTran->srBRec.inTranCardType == CREDIT_CARD || pobTran->srBRec.inTranCardType == PURCHASE_CARD || pobTran->srBRec.inTranCardType == COMMERCIAL_CARD);

    switch (pobTran->srTRTRec.TransactionCode) {
    case PHONE:
        inRetVal = inSelectWebPhone(pobTran, inCardAllowed);
        break;

    case SALE_OFFLINE:
        inRetVal = inSelectWebOffline(pobTran, inCardAllowed);
        break;

    case REFUND:
        inRetVal = inSelectWebRefund(pobTran, inCardAllowed);
        break;
    }

    return(inRetVal);
}
#endif /* #ifndef REMOVE_CORE_GET_WEB_MENU */

/*-------------------------------------------------------------------------
    Function :    inSelectWebPhone
    Description : Web selection menu for Phone transaction.  If ECI is
                  enabled, prompts for tran type Phone or Web.
    Parameters :
    Returns :   VS_SUCCESS, VS_ESCAPE
    Globals :
    Notes :     Sets the tran abbrev in srBRec for detail report.
    Modification History :
      #     Date      Who                     Description
    ----- -------- ---------- ---------------------------------------------
      0   12/4/00  Jeannie_C1 Created
--------------------------------------------------------------------------*/

int inSelectWebPhone(TRANSACTION_OBJECT* pobTran,int inCardAllowed) {
    int inECIType;
signed int     inChoice;

    pdebug(("--inSelectWebPhone--"));

    inECIType = inGetECISupport();

    if (!inCardAllowed || (inECIType == NO_ECI || inECIType > ECI_TYPE_SET_WITH_CERT)) {
        pdebug(("inCardAllowed=%d", inCardAllowed));
        pdebug(("inECIType=%d", inECIType));
        return (VS_SUCCESS);
    }

    vdClearNonTitleLines();
    if ((inChoice = obDisp.inDO_ChooseOne(NULL, pchGetMsg(WEB_CHOOSE_TRAN_MSG), pchGetMsg(WEB_PHONE_MSG), 2, NULL_PFI)) == VS_ESCAPE)
        return(VS_ESCAPE);

    if (inChoice == 2)                 /* Web sale selected */ {
        vdDisplayTranTitle(WEB_SALE_TRAN_MSG);

        if (inGetECI(pobTran) == VS_ESCAPE)
            return(VS_ESCAPE);

        pobTran->srBRec.lnMessage = WEB_SALE_TRAN_MSG;
        strlcpy(pobTran->srBRec.szTranAbbrev, (char *) pchGetMsg(WEB_PHONE_ABBREV_MSG), TRT_ABBREV_SIZE);
        pobTran->srBRec.fWebTran = VS_TRUE;
    } else                                /* Regular Phone Order */ {
        vdDisplayTranTitle(PHONE_MSG);
        pobTran->srBRec.lnMessage = PHONE_MSG;
    }

    return(VS_SUCCESS);
}


/*-------------------------------------------------------------------------
    Function :    inSelectWebOffline
    Description : Web selection menu for Offline transaction if account
                  number was entered manually.  If ECI is enabled, prompts
                  for tran types Sale, Phone or Web.  If ECI is disabled,
                  prompts for tran type Sale or Phone.
    Parameters :
    Returns :   VS_SUCCESS, VS_ESCAPE
    Globals :
    Notes :     Sets the tran abbrev in srBRec for detail report.
    Modification History :
      #     Date      Who                     Description
    ----- -------- ---------- ---------------------------------------------
      0   12/4/00  Jeannie_C1 Created
--------------------------------------------------------------------------*/

int inSelectWebOffline(TRANSACTION_OBJECT* pobTran,int inCardAllowed) {
    int inECIType;
    signed int inChoice;

    inECIType = inGetECISupport();
    vdClearNonTitleLines();

    if (inCardAllowed && (inECIType > NO_ECI && inECIType <= ECI_TYPE_SET_WITH_CERT)) {
        if ((inChoice = obDisp.inDO_ChooseOne(NULL, pchGetMsg(WEB_CHOOSE_TRAN_MSG), pchGetMsg(WEB_SALE_PHONE_MSG), 3, NULL_PFI)) == VS_ESCAPE)
            return(VS_ESCAPE);
    } else {
        if ((inChoice = obDisp.inDO_ChooseOne(NULL, pchGetMsg(WEB_CHOOSE_TRAN_MSG), pchGetMsg(SALE_PHONE_MSG), 2, NULL_PFI)) == VS_ESCAPE)
            return(VS_ESCAPE);
    }

    if (inChoice == 2)                 /* Offline Phone selected */ {
        vdDisplayTranTitle(OFFLINE_PHONE_TRAN_MSG);
        pobTran->srBRec.lnMessage = OFFLINE_PHONE_TRAN_MSG;
        strlcpy(pobTran->srBRec.szTranAbbrev, (char *) pchGetMsg(OFFLINE_PHONE_ABBREV_MSG), TRT_ABBREV_SIZE);
    }

    if (inChoice == 3)                 /* Offline Web selected */ {
        vdDisplayTranTitle(WEB_OFFLINE_TRAN_MSG);

        if (inGetECI(pobTran) == VS_ESCAPE)
            return(VS_ESCAPE);

        pobTran->srBRec.lnMessage = WEB_OFFLINE_TRAN_MSG;
        strlcpy(pobTran->srBRec.szTranAbbrev, (char *) pchGetMsg(WEB_OFFLINE_ABBREV_MSG), TRT_ABBREV_SIZE);
        pobTran->srBRec.fWebTran = VS_TRUE;
    }

    return(VS_SUCCESS);
}


/*-------------------------------------------------------------------------
    Function :    inSelectWebRefund
    Description : Web selection menu for Refund transaction if account
                  number was entered manually.  If ECI is enabled,
                  prompts for tran types Sale or Web.
    Parameters :
    Returns :   VS_SUCCESS, VS_ESCAPE
    Globals :
    Notes :     Sets the tran abbrev in srBRec for detail report.
    Modification History :
      #     Date      Who                     Description
    ----- -------- ---------- ---------------------------------------------
      0   12/4/00  Jeannie_C1 Created
--------------------------------------------------------------------------*/

int inSelectWebRefund(TRANSACTION_OBJECT* pobTran,int inCardAllowed) {
    int inECIType;
    signed int inChoice;

    inECIType = inGetECISupport();

    if (!inCardAllowed || (inECIType == NO_ECI || inECIType > ECI_TYPE_SET_WITH_CERT))
        return (VS_SUCCESS);

    vdClearNonTitleLines();
    if ((inChoice = obDisp.inDO_ChooseOne(NULL, pchGetMsg(WEB_CHOOSE_TRAN_MSG), pchGetMsg(WEB_SALE_MSG), 2, NULL_PFI)) == VS_ESCAPE)
        return(VS_ESCAPE);

    if (inChoice == 2)                 /* Web Refund selected */ {
        vdDisplayTranTitle(WEB_REFUND_TRAN_MSG);

        if (inGetECI(pobTran) == VS_ESCAPE)
            return(VS_ESCAPE);

        pobTran->srBRec.lnMessage = WEB_REFUND_TRAN_MSG;
        strlcpy(pobTran->srBRec.szTranAbbrev, (char *) pchGetMsg(WEB_REFUND_ABBREV_MSG), TRT_ABBREV_SIZE);
        pobTran->srBRec.fWebTran = VS_TRUE;
    }

    return(VS_SUCCESS);
}


/*-------------------------------------------------------------------------
    Function :    inGetECI
    Description : If ECI flag is set to '1' prompt for encrypted or not
                  set ECI accordingly.  Else set ECI base on the ECI flag.
    Parameters :
    Returns :   VS_SUCCESS, VS_ESCAPE
    Globals :
    Notes :
    Modification History :
      #     Date      Who                     Description
    ----- -------- ---------- ---------------------------------------------
      0   12/4/00  Jeannie_C1 Created
--------------------------------------------------------------------------*/

int inGetECI(TRANSACTION_OBJECT* pobTran) {
    signed int inChoice;

    switch (inGetECISupport()) {
    case ECI_PROMPT_ENCRYPT:
        if ((inChoice = obDisp.inDO_ChooseOne(NULL, pchGetMsg(ECI_ENCRYPTED_MSG), pchGetMsg(YESNO_MSG5x), 2, NULL_PFI)) == VS_ESCAPE)
            return(VS_ESCAPE);

        if (inChoice == KEY_NO)
            pobTran->srBRec.chECI = ECI_NON_SECURED;
        else
            pobTran->srBRec.chECI = ECI_SSL_SECURED;
        break;

    case ECI_TYPE_SECURED:
        pobTran->srBRec.chECI = ECI_SSL_SECURED;
        break;

    case ECI_TYPE_NON_SECURED:
        pobTran->srBRec.chECI = ECI_NON_SECURED;
        break;

    case ECI_TYPE_SET_NO_CERT:
        pobTran->srBRec.chECI = ECI_SET_NO_CERT;
        break;

    case ECI_TYPE_SET_WITH_CERT:
        pobTran->srBRec.chECI = ECI_SET_WITH_CERT;
        break;
    }

    return(VS_SUCCESS);
}


int FIELDS2_CODE_CHECK(char* a,char* b,short* c,char* d,unsigned char* e) {
    GET_VER(a, b, c, d, e);

    return 1;
}


#undef VSFIELD2_C

